<?php
class Db
{
	
	public $_linkid = null;
	
	public static $_instance = null;
	
	public $_lastSql = '';
	
	public $_error = '';
	
	private function __construct()
	{
		$this->_linkid = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('count connect to mysql server');
		mysql_select_db(DB_NAME, $this->_linkid) or die(mysql_error($this->_linkid));
		mysql_query("SET NAMES 'utf8'");
	}
	
	public static function getInstance()
	{
		if(!isset(self::$_instance) || !(self::$_instance instanceof self)){
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public function query($sql)
	{
		$this->_lastSql = $sql;
		$result = mysql_query($this->_lastSql, $this->_linkid);
		if($result){
			return $result;
		}else{
			$this->_error = mysql_error($this->_linkid);
			if(ENV==2){
				exit("<b>SQL:</b>{$sql}<br/><b>ERROR:</b>{$this->_error}");
			}
			return FALSE;
		}
	}
	
	public function getLastInsertId()
	{
		return mysql_insert_id($this->_linkid);
	}
	
	public function getLastSql()
	{
		return $this->_lastSql;
	}
	
	public function getError()
	{
		return mysql_error($this->_linkid);
	}
}